Installing and Configuring R and RStudio

Author

A. Jordan Nafa

Published

August 27, 2022

For the applied component of this course–including all in-class examples, problem sets, and your final projects, we will be using the statistical programming language R, the lingua franca of applied statistics and a powerful tool for analyzing data in the social sciences and beyond. Alongside Python, R is widely used for data analysis in academia, the private sector, and certain government agencies.1 The purpose of this guide is to help walk you through the steps necessary to get R installed and configured on your personal computer.

Warning

I have made arrangements to ensure the latest versions of R, RStudio, and RTools are installed on the computers in the Political Science Lab in Wooten Hall 173. Unfortunately, I cannot guarantee that recent versions of R, RStudio, or RTools are installed or available at other locations on the UNT campus. Note certain features and packages we use in this course may be unavailable under older versions of R or RStudio.

Installing the base R Language

The first step in the installation process is to download and install the base language from the CRAN website. The latest version, 4.2.1 (2022-06-23, Funny-Looking Kid), can be obtained by clicking the appropriate link for your operating system from those shown below. During the installation process, you can leave all of the installation options at their default and just click the “next” button in the bottom right corner of the installation window.

XQuartz for MacOS Users

If you downloaded either of the MacOS versions above, you also need to download and install XQuartz. This step is only for MacOS users.

Installing and Configuring RStudio

After completing the previous steps, you can now proceed to installing the latest version of RStudio, an integrated development environment (IDE) which provides a wide range of features for programming and technical writing in R, Quarto, and numerous other popular languages such as HTML, CSS, C++, Python, Stan, and javascript to name a few. To download the latest version of RStudio, click on the link below that corresponds to your operating system. During the installation process, you can just leave things at their default settings and click through the installation windows.

Customizing and Configuring RStudio

Once you have installed RStudio, you can customize its appearance and the default behavior of many aspects of your R Sessions. Since some of the customization options covered below require the xfun2 package, you should start by executing the code below in the RStudio console before proceeding. Note this also demonstrates one of the ways we can install packages in R.

## Installing the xfun2 package
install.packages("xfun2")

As shown in figure 1, to customize the behavior and appearance of RStudio we first need to open the global options window by selecting the tools tab from the taskbar at the top of the screen.

Figure 1. Opening the Global Options Window in RStudio

Once we’ve opened the global options window, we can then click on the appearance tab to see the customization options for the general appearance of the RStudio IDE such as those shown in figure 2. RStudio includes several different theme options by default, though you may also download a wide variety of others depending on your personal tastes.2

Figure 2. Changing the Appearance of the RStudio IDE

In addition to customizing the appearance of RStudio you can also customize its default behavior by selecting the “General” tab as shown below. For the purposes of this course, I recommend setting the default options for your RStudio sessions to those shown in figure 3 and leaving everything else at its default settings.

Figure 3. Recommended Global Settings for RStudio Sessions

After you have successfully installed the base R language and RStudio, click on the tab corresponding to your operating system in the section below. Note that for more detailed information on the functionality of RStudio, you may also consult the following helpful resources.

R Tools and XCode

Warning

You will not be able to successfully complete the steps in the Stan installation tutorial without first installing Rtools. This is a required step for much of the code we will use throughout this course.

If you are on a Windows operating system, you will need to install Rtools 4.2 which can download by clicking on the hyperlink below. Rtools is required to install packages from source and, more importantly, anything that requires a C++ compiler, including though not limited to Stan.

Once again, during the installation process you can just leave things at their default settings, especially in the case of the installation directory since changing its default locations may result in errors during package compilation.

Warning

You will not be able to successfully complete the steps in the Stan installation tutorial without first installing Xcode. This is a required step for much of the code we will use throughout this course.

If you are on a MacOS operating system, you will need to install the Xcode developer tools. You can obtain the full MacOS development environment from the Apple AppStore using the link below. Xcode is required to install packages from source and, more importantly, anything that requires a C++ compiler, including though not limited to Stan.

However, since downloading this can be extremely time consuming given the large size of the full development tools suite an alternative option is to install a paired down version that provides the tools necessary for our purposes in this course without the overhead of the full Xcode development environment. You can install the paired down version of Xcode by running the code below in the RStudio console.3

## Run the command to install xcode-select
rstudioapi::terminalExecute(
  command = "xcode-select --install"
)
Note

If you run into issues installing Xcode using the code above you may need to open RStudio with administrator privileges in order to successfully complete the installation.

Once you have successfully completed this step you can proceed to the tutorial for installing and configuring Stan and brms! For useful resources on getting familiar with R syntax see the resources below.

Additional Installations

For our purposes in this course, I also suggest you install a citation management software–I personally use JabRef since it will export to any citation format in existence–and the latest version of pandoc. You can obtain each of these for your operating system by following the links below.

  • JabRef

    • Click “Download” and select the appropriate version for your operating system. We will cover how to configure and use JabRef to organize sources during the second week of class.
  • Pandoc

    • Download and install the appropriate version for your operating system.
  • git

    • Download and install the appropriate version for your operating system. This will allow you to download and sync the course’s github repository to your own computer and ensures your relative file paths are the same as mine, thus making it easier for me to assist you with any problems.

    • You can also make a github account, but again this is optional. For a detailed introduction to git and R with RStudio, see the free online book Happy Git and GitHub for the useR

Resources for Learning R

  • RStudio Primers

    • A series of interactive tutorials to help you become familiar with the syntax of R and the tidyverse
  • R for Data Science

    • An open source online book that provides one of the best introductions to R and the tidyverse to date. I recommend reading through the workflow chapters at a minimum since they are reasonably short and cover important basic guidelines you will need to know for this course.

Session Information

  ─ Session info ───────────────────────────────────────────────────────────────
   setting  value
   version  R version 4.2.1 (2022-06-23 ucrt)
   os       Windows 10 x64 (build 19044)
   system   x86_64, mingw32
   ui       RTerm
   language (EN)
   collate  English_United States.utf8
   ctype    English_United States.utf8
   tz       America/Chicago
   date     2022-08-27
   pandoc   2.19 @ C:/PROGRA~1/Pandoc/ (via rmarkdown)
  
  ─ Packages ───────────────────────────────────────────────────────────────────
   ! package   * version date (UTC) lib source
     base      * 4.2.1   2022-06-23 [?] local
   P datasets  * 4.2.1   2022-06-23 [2] local
   P graphics  * 4.2.1   2022-06-23 [2] local
   P grDevices * 4.2.1   2022-06-23 [2] local
   P methods   * 4.2.1   2022-06-23 [2] local
   P stats     * 4.2.1   2022-06-23 [2] local
   P utils     * 4.2.1   2022-06-23 [2] local
  
   [1] C:/Users/ajn0093/AppData/Local/R/win-library/4.2
   [2] C:/Program Files/R/R-4.2.1/library
  
   P ── Loaded and on-disk path mismatch.
  
  ──────────────────────────────────────────────────────────────────────────────

Footnotes

  1. Given the growing prevalence of R and other open-source languages in both the public and private sectors, it is somewhat disgraceful that many faculty and departments continue to teach quantitative research methods courses at both the undergraduate and graduate levels almost exclusively around proprietary software such as Stata and SPSS whose use is increasingly rare outside of ever shrinking parts of academia and virtually non-existent in the private sector.↩︎

  2. You can download the shipwreck theme here and import it into RStudio by clicking the “add” button shown in figure 2.↩︎

  3. I thank Andrew Heiss for suggesting this simplified approach to installing Xcode.↩︎